package com.longway.common.controller;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.IDataLoader;
import com.longway.core.toolbox.support.SqlKeyword;

import java.util.List;
import java.util.Map;

public class SelectController extends BaseController{

	public void index() {
	}

	/**
	 * 根据字典编号获取下拉框
	 * @return String
	 */
	public void getSelectByCode() {
		final String code = getPara("code");
		List<Record> dict = CacheKit.get(DICT_CACHE, "dict_common_" + code,
				new IDataLoader() {
					public List<Record> load() {
						return Db.find("select num as id,pid as pid,name from  base_dict where code=? and num <> '-1' order by xssx",code);
					}
				});
		renderJson(ajaxSucess(dict));
	}
	
	public void getSelectNameById() {
		final String id = getPara("id");
		final String code = getPara("code");
		Record dict = CacheKit.get(DICT_CACHE, "dictname_"+code+"_"+id,
				new IDataLoader() {
					public Record load() {
						return Db.findFirst("select num as id,pid as pid,name from base_dict where code=? and num = ?",code,id);
					}
				});
		renderJson(ajaxSucess(dict));
	}
	/**
	 * 获取部门信息
	 */
	public void getDeptSelect() {
		String parameter = getPara("parameter");
		Map<String, String> mapparam = SqlKeyword.getSqlMapDeCode2(parameter);	
		SqlPara sqlSource = getSql("dept", "dept.selectDept" ,mapparam);
		List<Record> dept = Db.find(sqlSource); 
		renderJson(ajaxSucess(dept));
	}
	
	

	
	
	public void getDeptSelectNameById() {
		final String id = getPara("id");
		Record dept = CacheKit.get(DEPT_CACHE, "deptname_" + id,
				new IDataLoader() {
					public Record load() {
						return Db.findFirst("select id,pid,simpleName as name from base_dept where id = ?",id);
					}
				});
		renderJson(ajaxSucess(dept));
	}
	
	public void getUserSelect() {
		String parameter = getPara("parameter");
		Map<String, String> mapparam = SqlKeyword.getSqlMapDeCode2(parameter);	
		SqlPara sqlSource = getSql("user", "user.selectUser" ,mapparam);
		List<Record> user = Db.find(sqlSource); 
		renderJson(ajaxSucess(user));
	}
	
	public void getUserSelectNameById() {
		final String id = getPara("id");
		Record user = CacheKit.get(USER_CACHE, "username_" + id,
				new IDataLoader() {
					public Record load() {
						return Db.findFirst("select id,name from base_user where id = ?",id);
					}
				});
		renderJson(ajaxSucess(user));
	}
	
	public void getRoleSelect() {
		String parameter = getPara("parameter");
		Map<String, String> mapparam = SqlKeyword.getSqlMapDeCode2(parameter);	
		SqlPara sqlSource = getSql("role", "role.selectRole" ,mapparam);
		List<Record> role = Db.find(sqlSource); 
		renderJson(ajaxSucess(role));
	}
	
	public void getRoleSelectNameById() {
		final String id = getPara("id");
		Record role = CacheKit.get(ROLE_CACHE, "rolename_" + id,
				new IDataLoader() {
					public Record load() {
						return Db.findFirst("select id,name from base_role where id = ?",id);
					}
				});
		renderJson(ajaxSucess(role));
	}
	
	public void getSelectDiy() {
		String parameter = getPara("parameter");
		String source = getPara("source");
		Map<String, String> mapparam = SqlKeyword.getSqlMapDeCode2(parameter);	
		SqlPara sqlSource = getSql("diy", source ,mapparam);
		List<Record> selectDiy = Db.find(sqlSource); 
		renderJson(ajaxSucess(selectDiy));
	}
	
	/**
	 * 根据参数查询级联列表
	 */
	public void getcascadeListByParam() {
		//cascadeType,source,parameter
		String type = getPara("cascadeType");     //级联类型
		String source = getPara("source");   //级联数据源
		//级联查询参数
		String parameter = getPara("parameter");
		Map<String, String> mapparam = SqlKeyword.getSqlMapDeCode2(parameter);
		if("dict".equals(type)){
			source = "dict.selectDict";
		}else if("area".equals(type)){
			source = "city.selectArea";
		}
		SqlPara sqlPara = getSql(type,source,mapparam);
		List<Record> data = null;
		//如果是数据字典和区域则走缓存
		if("dict".equals(type)){
			Object pid = mapparam.get("pid");
			data = CacheKit.get(DICT_CACHE, "cascade_"+type+"_"+pid,
					new IDataLoader() {
						public List<Record> load() {
							return Db.find(sqlPara);
						}
					});
		}else if("area".equals(type)){
			Object pid = mapparam.get("pid");
			data = CacheKit.get(AREA_CACHE, "cascade_"+type+"_"+pid,
					new IDataLoader() {
						public List<Record> load() {
							return Db.find(sqlPara);
						}
					});
		}else{
			data = Db.find(sqlPara);
		}
		renderJson(ajaxSucess(data));
	}
	
	public void getcascadeNameById(){
		String type = getPara("cascadeType");     //级联类型
		String source = getPara("source");   //级联数据源
		//级联查询参数
		String parameter = getPara("parameter");
		Map<String, String> mapparam = SqlKeyword.getSqlMapDeCode2(parameter);
		if("dict".equals(type)){
			source = "dict.selectDict";
		}else if("area".equals(type)){
			source = "city.selectArea";
		}
		SqlPara sqlPara = getSql(type,source,mapparam);
		Record data = null;
		//如果是数据字典和区域则走缓存
		if("dict".equals(type)){
			Object id = mapparam.get("id");
			data = CacheKit.get(DIY_CACHE, "cascadename_"+id,
					new IDataLoader() {
						public Record load() {
							return Db.findFirst(sqlPara);
						}
					});
		}else if("area".equals(type)){
			Object areaCode = mapparam.get("areaCode");
			data = CacheKit.get(AREA_CACHE, "cascadename_"+areaCode,
					new IDataLoader() {
						public Record load() {
							return Db.findFirst(sqlPara);
						}
					});
		}else{
			data = Db.findFirst(sqlPara);
		}
		renderJson(ajaxSucess(data));
	}
	
	private SqlPara getSql(String type ,String source,Map<String, String> mapparam){
		SqlPara sqlPara = null;
		sqlPara = Db.getSqlPara(source,mapparam);
		return sqlPara;
	}

	/**
	 * 搜索框展示所有企业
	 */
	public void getQyglSelectById() {
		String roleId = getLoginAccount().getRoleId();
		String sql = "select qymc as name,id from lhyg_qygl order by createTime desc";
		if (INTERCEPT_ROLE_ID.get(roleId) != null) {
			sql = "select qymc as name,id from lhyg_qygl where createById = " + getLoginAccountId() + " order by createTime desc";
		}
		List<Record> dept = Db.find(sql);
		renderJson(ajaxSucess(dept));
	}

	/**
	 * 搜索框只展示发放过佣金的企业
	 */
	public void getFfQyglSelectById() {
		String roleId = getLoginAccount().getRoleId();
		String sql = "select qygl.qymc name,qygl.id id from lhyg_qygl qygl inner join lhyg_ffjl ffjl on ffjl.qyId = qygl.id group by qygl.id order by qygl.createTime desc";
		if (INTERCEPT_ROLE_ID.get(roleId) != null) {
			sql = "select qygl.qymc name,qygl.id id from lhyg_qygl qygl inner join lhyg_ffjl ffjl on ffjl.qyId = qygl.id where qygl.createById = " + getLoginAccountId() + " group by qygl.id order by qygl.createTime desc";
		}
		List<Record> dept = Db.find(sql);
		renderJson(ajaxSucess(dept));
	}

	/**
	 * 搜索框只展示可开票的企业
	 */
	public void getTaxQyglSelectById() {
		String roleId = getLoginAccount().getRoleId();
		String sql = "select qygl.qymc name,qygl.id id from lhyg_qygl qygl inner join lhyg_ffjl ffjl on ffjl.qyId = qygl.id where ffjl.state in (3,4) and ffjl.rate < 5 group by qygl.id order by qygl.createTime desc";
		if (INTERCEPT_ROLE_ID.get(roleId) != null) {
			sql = "select qygl.qymc name,qygl.id id from lhyg_qygl qygl inner join lhyg_ffjl ffjl on ffjl.qyId = qygl.id where ffjl.state in (3,4) and ffjl.rate < 5 and qygl.createById = " + getLoginAccountId() + " group by qygl.id order by qygl.createTime desc";
		}
		List<Record> dept = Db.find(sql);
		renderJson(ajaxSucess(dept));
	}

}
